package com.caiyigo.baihua.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.http.client.ClientProtocolException;
import org.sageras.util.ExcelUtil.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.caiyigo.baihua.Const.Dictionary;
import com.caiyigo.baihua.dto.DataGrid;
import com.caiyigo.baihua.dto.KaoshicjDto;
import com.caiyigo.baihua.dto.KaoshizwDto;
import com.caiyigo.baihua.dto.Pager;
import com.caiyigo.baihua.dto.Result;
import com.caiyigo.baihua.model.Xuesheng;
import com.caiyigo.baihua.service.KaoshizwService;
import com.caiyigo.baihua.service.XueshengService;
import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Controller
@RequestMapping(value = "/business/kaoshizw")
public class KaoshizwController {
	@Autowired
	KaoshizwService kaoshizwService;

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public @ResponseBody DataGrid<KaoshizwDto> list(KaoshizwDto dto, Pager page) {
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.create(page.getSort(), page.getOrder()));
		PageList<KaoshizwDto> pageList = (PageList<KaoshizwDto>) kaoshizwService
				.getList(dto, new PageBounds(page.getPage(), page.getRows(),
						orders, true));
		for (KaoshizwDto kaoshicjDto : pageList) {
			kaoshicjDto.setIsSendReasonMc(Dictionary.FSZTYY.getName(kaoshicjDto
					.getIsSendReason()));
			kaoshicjDto.setIsSendMc(Dictionary.FSZT.getName(kaoshicjDto
					.getIsSend()));
		}
		return new DataGrid<KaoshizwDto>(pageList.getPaginator()
				.getTotalCount(), pageList.getPaginator().getPage(), pageList);

	}

	@RequestMapping(value = "/export", method = RequestMethod.GET)
	public void export(KaoshizwDto dto, Pager page, HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		String fileName = "baihua-kaoshizw-" + System.currentTimeMillis()
				+ ".xls";
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.create(KaoshizwDto.FileConstant.zkz, "desc"));
		List<KaoshizwDto> KaoshizwDtopageList = kaoshizwService.getList(dto,
				new PageBounds(orders));
		for (KaoshizwDto kaoshizwDto : KaoshizwDtopageList) {
			kaoshizwDto.setIsSend(Dictionary.FSZT.getName(kaoshizwDto
					.getIsSend()));
			kaoshizwDto.setIsSendReason(Dictionary.FSZT.getName(kaoshizwDto
					.getIsSendReason()));
		}
		response.setHeader("Content-Disposition", "inline; filename="
				+ fileName);
		// 确保发送的当前文本格式
		response.setContentType("application/vnd.ms-excel");
		try {
			ExcelUtil.exportExcel("百花教育考试座位表", KaoshizwDtopageList,
					response.getOutputStream(), KaoshizwDto.class);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@RequestMapping(value = "/select", method = RequestMethod.POST)
	public @ResponseBody DataGrid<KaoshizwDto> select(KaoshizwDto dto,
			DataGrid<KaoshizwDto> dataGrid) {
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.create(dataGrid.getSort(), dataGrid.getOrder()));
		PageList<KaoshizwDto> pageList = (PageList<KaoshizwDto>) kaoshizwService
				.getList(dto, new PageBounds(1, 11, orders, true));
		dataGrid.setPage(pageList.getPaginator().getPage());
		dataGrid.setRows(pageList);
		return dataGrid;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Result delete(
			@RequestParam(value = "id", required = true) Integer id) {

		return new Result(kaoshizwService.deleteByPrimaryKey(id) > 0);

	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody Result add(@ModelAttribute("user") KaoshizwDto dto) {

		return new Result(kaoshizwService.insertSelective(dto) > 0);

	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public @ResponseBody Result edit(@ModelAttribute("user") KaoshizwDto dto) {

		return new Result(kaoshizwService.updateByPrimaryKeySelective(dto) > 0);

	}

	@RequestMapping(value = "/batchDelete", method = RequestMethod.GET)
	public @ResponseBody Result batchDelete(
			@RequestParam(value = "ids", required = false) String id) {
		String[] idArray = id.split(",");

		return new Result(kaoshizwService.batchDelete(idArray) > 0);

	}

	@RequestMapping(value = "/getzw", method = RequestMethod.POST)
	public @ResponseBody Result getzw(String pici) {
		Map<String, Xuesheng> xueMap = xueshengService.getXueshengForZWCJ(pici,
				"zw");
		try {
			Result result = CjZwUtil.get(xueMap, pici, true, true);
			List<Future<List<Object>>> resultFutureList = (List<Future<List<Object>>>) result
					.getData();
			List<KaoshizwDto> list = new ArrayList<KaoshizwDto>();
			for (Future<List<Object>> future : resultFutureList) {

				List<Object> objects = null;

				try {
					objects = future.get();
					if (null != objects) {
						for (Object object : objects) {
							KaoshizwDto cjDto = (KaoshizwDto) object;
							if (StringUtils.isNotEmpty(cjDto.getName())) {
								cjDto.setXsid(xueMap.get(cjDto.getZkz())
										.getId());
							
								cjDto.setAddtime(new Date().toGMTString());
								list.add(cjDto);
							}
							System.out.println(cjDto);
						}
					}

				} catch (InterruptedException e) {

					e.printStackTrace();
					continue;
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					continue;
				}

			}
			if (list.size() > 0) {
				kaoshizwService.batchUpdate(list);
			}

			return new Result(true);

		} catch (ClientProtocolException e) {
			return new Result(false);
		} catch (IOException e) {
			return new Result(false);
		}
	}

	@Autowired
	XueshengService xueshengService;
}
